package com.inspinia.base.mq.util;


import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

import javax.jms.*;
import java.io.Serializable;
import java.util.Map;

/**
 * Created by veblen on 16/11/24.
 */
public class JmsUtil {

    /**
     * 延迟发送对象消息
     * @param jmsTemplate
     * @param queueName
     * @param objectMessage
     * @param delay
     */
    public static void sendMessageDelay(JmsTemplate jmsTemplate , final String queueName, final Serializable objectMessage, final long delay) {
        jmsTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                ObjectMessage om = session.createObjectMessage(objectMessage);
//                om.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY, delay);
//                om.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_PERIOD, 1 * 1000);
//                om.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_REPEAT, 1);
                return om;
            }
        });
    }

    /**
     * 发送文本消息
     * @param jmsQueueTemplate
     * @param queueName
     * @param txtMessage
     */
    public static void sendTextMessage(JmsTemplate jmsQueueTemplate,final String queueName, final String txtMessage) {
        jmsQueueTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(txtMessage);
            }
        });
    }

    // 发送对象消息
    public static void sendObjectMessage(JmsTemplate jmsQueueTemplate,final String queueName, final Object objectMessage) {
        jmsQueueTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage((Serializable) objectMessage);
            }
        });
    }

    // 发送Map消息
    public static void sendMapMessage(JmsTemplate jmsQueueTemplate,final String queueName, final Map<String, Object> mapMessage) {
        jmsQueueTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                MapMessage mapMessage = session.createMapMessage();
                for (Map.Entry<String, Object> entry : ((Map<String, Object>) mapMessage).entrySet()) {
                    mapMessage.setObject(entry.getKey(), entry.getValue());
                }
                return mapMessage;
            }
        });
    }

    // 发送字节消息
    public static void sendByteMessage(JmsTemplate jmsQueueTemplate,final String queueName, final byte[] message) {
        jmsQueueTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                BytesMessage bytesMessage = session.createBytesMessage();
                bytesMessage.writeBytes(message);
                return bytesMessage;
            }
        });
    }

    // 发送流消息
    public static void sendStreamMessage(JmsTemplate jmsQueueTemplate,final String queueName, final Object message) {
        jmsQueueTemplate.send(queueName, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                StreamMessage streamMessage = session.createStreamMessage();
                streamMessage.writeObject(message);
                return streamMessage;
            }
        });
    }

}
